<h3>{{ labels.Title  + (!readonly ? "  |  " : "")}}
    <a v-if="!readonly" style="cursor:pointer" @click="add()">{{ ButtonTextCreate }}</a>
</h3>
<div id="WorkflowDesignerActorsList" class="WorkflowDesignerWindowForm">
    <div style="display: flex; justify-content: flex-end; margin-bottom: 5px;">
        <el-switch  v-model="showPredefined" :active-text="labels.ShowPredefinedActors"></el-switch>
    </div>
    <table class="WorkflowDesignerTable">
        <th>
            <td v-if="isIE"></td>
            <td><b>{{labels.Name}}</b></td>
            <td><b>{{labels.Rule}}</b></td>
            <td><b>{{labels.Value}}</b></td>
        </th>
        <tr v-for="(item, index) in items" v-if="showPredefined || item.IsPredefined!=true"
            :class="dragOverIndex == index && dragOverBlock  == item ? 'dragOver' : ''"
            @dragstart="dragstart(index, $event)" @dragend="dragend($event)"
            @dragover="dragover(item, index, $event)">
            <td class='WorkflowDesignerTableMoveCol' :draggable="!readonly" ><div v-if="!readonly" class='WorkflowDesignerTableMoveButton'></div></td>
            <td>
                <el-input
                    v-model="item.Name" 
                    :title="validateField('Name', item)" 
                    :class="validateField('Name', item) ? 'WorkflowDesignerInputError' : ''"
                    :readonly="readonly || item.IsPredefined === true"
                ></el-input>
            </td>
            <td>
                <el-select 
                    v-model="item.Rule" 
                    style="width: 100%;" 
                    :disabled="readonly || item.IsPredefined === true"
                    filterable clearable 
                    placeholder="" 
                    :title="validateField('Rule', item)" 
                    :class="validateField('Rule', item) ? 'WorkflowDesignerInputError' : ''">
                    <el-option v-for="item in conditions" :key="item" :label="item" :value="item"></el-option>
                </el-select>
            </td>
            <td>
                <el-autocomplete v-model="item.Value" :readonly="readonly || item.IsPredefined === true" :fetch-suggestions="querySearch" @focus="setCurrentItem(item)"></el-autocomplete>
            </td>

            <td v-if="!readonly" class="WorkflowDesignerTableEditButtons Double">
                <el-button-group>
                    <el-button @click="showjson('Value', item)" v-if="item.IsPredefined != true" :class="'WorkflowDesignerTableCodeActionsButton ' + (editItem == item ? 'is-active' : '')"></el-button>
                    <el-button @click="remove(index)"  v-if="item.IsPredefined != true" class="WorkflowDesignerTableDeleteButton"></el-button>
                </el-button-group>
            </td>
        </tr>
    </table>
</div>
<div class="WorkflowDesignerButtons">
    <el-button v-if="!readonly" @click="onSave" type="primary">{{ ButtonTextSave }}</el-button>
    <el-button @click="onClose">{{ ButtonTextCancel }}</el-button>
</div>
<el-dialog
    :title="confirmdialog.title"
    :before-close="function() {confirmdialog.visible = false}"
    :visible="confirmdialog.visible"
    :modal-append-to-body="false"
    width="30%">
    <span>{{ confirmdialog.message }}</span>
    <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="confirmdialog.onSuccess">{{confirmdialog.yes}}</el-button>
        <el-button @click="confirmdialog.visible = false">{{confirmdialog.no}}</el-button>
    </span>
</el-dialog> 
<script type="application/javascript">
    function actors_Init(me){
         me.VueConfig.data = Object.assign(me.VueConfig.data, {
            readonly: false,
            labels: WorkflowDesignerConstants.ActorFormLabel,
            editItem: undefined,     
            ButtonTextCreate: WorkflowDesignerConstants.ButtonTextCreate,
            ButtonTextDelete: WorkflowDesignerConstants.ButtonTextDelete,
            ButtonTextSave: WorkflowDesignerConstants.ButtonTextSave,
            ButtonTextCancel: WorkflowDesignerConstants.ButtonTextCancel,
            confirmdialog: {
                title: WorkflowDesignerConstants.DialogConfirmText,
                message: WorkflowDesignerConstants.CloseWithoutSaving,
                visible: false,
                yes: WorkflowDesignerConstants.ButtonTextYes,
                no: WorkflowDesignerConstants.ButtonTextNo,
                onSuccess: function(){
                    me.VueConfig.data.confirmdialog.visible = false;
                    me.onClose(true);
                }
            },
            showPredefined: false
        });

        me.VueConfig.methods.setCurrentItem = function(item) {
            this.currentItem = item;
        };
        me.VueConfig.methods.querySearch = function(queryString, cb) {
            if(me.VueConfig.data.readonly)
                return cb([]);
                
            var res = me.graph.getAutoCompleteSuggestions2("ruleparameter", this.currentItem.Rule, queryString);
            cb(res);
        };

        me.VueConfig.methods.onUpdate = function(){
            me.VueConfig.data.conditions = me.graph.getActorRules();
            me.VueConfig.data.items = WorkflowDesignerCommon.clone(me.graph.data.Actors);
            me.VueConfig.data.readonly = me.graph.Settings.readonly;
        };

        me.VueConfig.methods.add = function(){
            me.VueConfig.data.items.push({Name:"", Rule:"", Value:""});

            setTimeout(function(){
                var objDiv = document.getElementById("WorkflowDesignerActorsList");
                objDiv.scrollTop = objDiv.scrollHeight;
            },10);
        };

        me.VueConfig.methods.remove = function(index){
            me.VueConfig.data.items.splice(index, 1);
        };

        me.VueConfig.methods.showjson = function(name, item) {
            me.VueConfig.data.editItem = item;
            me.editItem = item;

            var onSuccess = function(value){
                if(me.editItem){
                    me.editItem[name] = value;
                    me.VueConfig.data.editItem = undefined;
                    delete me.editItem;
                }
            };

            var onClose = function(value){
                me.VueConfig.data.editItem = undefined;
            };

            var params = {
                name: item["Rule"],
                type: ['RuleCheck', 'RuleGet']
            };
            me.VueConfig.data.jsonform = me.showjson(item[name], params, onSuccess, onClose);
        };

        me.VueConfig.methods.validateField = function(name, item){
            if(name != 'Name' && name != 'Rule')
                return;

            if(!item[name]){
                return WorkflowDesignerConstants.FieldIsRequired;
            }

            if(name == "Name"){
                var res = me.VueConfig.data.items.filter(function(i){ return i != item && i.Name == item.Name });
                if(res.length > 0){
                    return WorkflowDesignerConstants.FieldMustBeUnique;
                }
            }
        };

        me.VueConfig.methods.onHideEvent = function(){
            me.VueConfig.data.confirmdialog.visible = false;
        }

        me.VueConfig.methods.validate = function(){
            var validateFunc = me.VueConfig.methods.validateField;
            var items = me.VueConfig.data.items;
            for(var i=0; i < items.length; i++){
                var item = items[i];
                if(validateFunc('Name', item) || validateFunc('Rule', item))
                    return false;
            }

            return true;
        };

        me.VueConfig.methods.onSave = function(){
            if(me.VueConfig.methods.validate && me.VueConfig.methods.validate()){
                me.graph.UpdateData(me.graph.data.Actors, me.VueConfig.data.items);
                me.onClose(true);
            }
        };

        me.VueConfig.methods.onClose = function(){
            me.VueConfig.data.confirmdialog.visible = false;

            if(me.VueConfig.data.readonly){
                me.onClose(true);
                return;
            }

            if (!WorkflowDesignerCommon.compareArray(
                me.graph.data.Actors, 
                me.VueConfig.data.items, 
                ["Name", "Rule", "Value"])) {
                
                me.VueConfig.data.confirmdialog.visible = true;
                return false;
            }
            else{
                me.onClose(true);
            }
        };
    };
</script>